<template>
	<view>
		<view class="u-menu-wrap">
			<c-side-category class="category" :customNumStyle="{ color: 'red' }" :list="tabbar2"
			 active-color="#54B4F5" :width="220" @change="change">
				<view>
					<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop"
					 :scroll-into-view="itemId">
						<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current == index ? 'u-tab-item-active' : '']"
						 :data-current="index" @tap.stop="swichMenu(index)">
							<text class="u-line-1">{{item.name}}</text>
						</view>
					</scroll-view>
				</view>
			</c-side-category>
		</view>
		<view class="bottom">
			<view class="clear" @click="clear">
				<text style="font-size: 32rpx;color: #333333;line-height: 84rpx;">清除</text>
			</view>
			<view class="confirm" @click="confirm">
				<text style="font-size: 32rpx;color: #FFFFFF;line-height: 84rpx;">确定</text>
			</view>
		</view>
	</view>
</template>

<script>
	
	import classifyData from '@/common/classify.data.js';
	export default {

		data() {
			return {
				scrollTop: 0, //tab标题的滚动条位置
				oldScrollTop: 0,
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				tabbar: classifyData,
				tabbar2: [{
					"name": "cccccc"
				}, {
					"name": "ssss"
				}, {
					"name": "xxxxx"
				}, {
					"name": "wwwww"
				}, {
					"name": "ssss"
				}, {
					"name": "xxxxx"
				}, {
					"name": "wwwww"
				}, {
					"name": "ssss"
				}, {
					"name": "xxxxx"
				}, {
					"name": "wwwww"
				}, {
					"name": "ssss"
				}, {
					"name": "xxxxx"
				}, {
					"name": "wwwww"
				}],
				menuItemPos: [],
				arr: [],
				scrollRightTop: 0 // 右边栏目scroll-view的滚动条高度
			}
		},
		methods: {
			change(e){
				console.log(e);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},

			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
				}
	},
	}
</script>
 
<style lang="scss" scoped>
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
		margin-bottom: 120rpx;
	}

	.u-tab-view {
		width: 480rpx;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.bottom {
		width: 100%;
		height: 120rpx;
		position: fixed;
		background-color: #FFFFFF;
		bottom: 0;
		display: flex;
		flex-direction: row;

		.clear {
			width: 220rpx;
			height: 84rpx;
			align-self: center;
			text-align: center;
			border-radius: 6px;
			border: 1px solid #E8EAEC;
			margin-left: 40rpx;
		}

		.text {
			font-size: 32rpx;
			color: #333333;
		}

		.confirm {
			width: 430rpx;
			height: 84rpx;
			background: linear-gradient(270deg, #1698F9 0%, #4CC5FC 100%);
			border-radius: 6px;
			align-self: center;
			text-align: center;
			margin-left: 30rpx;
		}
	}
</style>
